/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2024-02-21 13:50:50
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-04-08 17:56:32
 * @FilePath: /yt-devops/lib/pages/devices/device_tab/components/form_text_list.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:yt_devops/components/yt_form_text.dart';

import 'package:yt_devops/pages/devices/device_tab/model/device_item_model.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/func_utils.dart';

class FormTextList extends StatefulWidget {
  final List<DetailItemModel> items;
  final Map? dataSource;
  const FormTextList({super.key, required this.items, this.dataSource});

  @override
  State<FormTextList> createState() => _FormTextListState();
}

class _FormTextListState extends State<FormTextList> {
  TextEditingController controller = TextEditingController(text: '--');
  @override
  Widget build(BuildContext context) {
    return ListView.separated(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      itemBuilder: (context, index) => YtFormText(
        titleStyle: widget.items[index].labelStyles,
        title: widget.items[index].label,
        value:
            formateValue(widget.items[index].field, widget.items[index].format),
        hint: '',
      ),
      itemCount: widget.items.length,
      separatorBuilder: (context, index) => const Divider(
        color: Colour.dividerColor,
      ),
    );
  }

  String formateValue(String field, Function? formate) {
    print('ormate == null==' + field.toString());
    print('ormate == null==' + formate.toString());
    if (widget.dataSource == null || widget.dataSource![field] == null) {
      return '--';
    }
    String value = '';
    if (formate == null) {
      value = widget.dataSource![field]?.toString() ?? '--';
    } else {
      value = formate(widget.dataSource![field].toString());
      print('ormate == null==' + value.toString());
    }

    if (FuncUtils.isEmpty(value)) {
      return '--';
    } else {
      return value;
    }
  }
}
